package com.logic.landseaserver.ws;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.service.IWebsiteConfig;
import com.logic.landseaserver.ws.request.WebsiteConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * Created by Drake on 2017/4/19.
 */
@Path("/website")
@Api(value = "关于朗诗寓")
public class WebsiteConfigRest {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IWebsiteConfig websiteConfigService;

    @PUT
    @Path("/editWebconfig")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "编辑朗诗寓PC端配置")
    public Response updateOrder(@ApiParam(value = "param", required = true) WebsiteConfig config) {
        CommonResultMap map = new CommonResultMap();
        try {
            websiteConfigService.edit(config);
            map.setResult(CommonResult.SUCCESS, "edit order");
        } catch (Exception e) {
            logger.error("editWebconfig error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取朗诗寓PC端配置")
    public Response getWebsiteconfig() {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(websiteConfigService.get());
            map.setResult(CommonResult.SUCCESS, "success");
        } catch (Exception e) {
            logger.error("getWebsiteconfig error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
